var EchartsUtil = {};

/******************************************************************************
 *
 * 折线图
 *
 * ***************************************************************************/
EchartsUtil.line = function (id, data, option) {
    var names = [];
    var values = [];
    for (var i = 0; i < data.length; i++) {
        names.push(data[i].name);
        values.push(data[i].value);
    }

    var _option_ = {
        grid: {
            top: 40,
            bottom: 40,
            right:20
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: names,
            splitLine: {
                show: false
            }
        },
        yAxis: {
            type: 'value',
            axisLine: {
                show: false
            }
        },
        series: [{
            data: values,
            type: 'line',
            showSymbol: false,
            areaStyle: {
                color: {
                    type: 'linear',
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [{
                        offset: 0, color: '#FFD11B' // 0% 处的颜色
                    }, {
                        offset: 1, color: '#000' // 100% 处的颜色
                    }],
                    globalCoord: false // 缺省为 false
                }
            },
            lineStyle: {
                color: '#FFD11B',
                width: 3
            }
        }]
    };


    option = $.extend(true, {}, _option_, option);

    var myChart = echarts.init(document.getElementById(id), 'walden');

    myChart.setOption(option);

    return myChart;
}


/******************************************************************************
 *
 * 柱状图
 *
 * ***************************************************************************/
EchartsUtil.bar = function (id, option) {
    var _option_ = {
        itemStyle: {
            normal: {
                barBorderWidth: 0,
                barBorderColor: "#ccc"
            },
            emphasis: {
                barBorderWidth: 0,
                barBorderColor: "#ccc"
            }
        },
        tooltip: {},
        xAxis: {
            data: []
        },
        yAxis: {},
        series: [{
            type: 'bar',
            barMaxWidth: 20,
            data: []
        }]
    };

    option = $.extend(true, {}, _option_, option);

    var myChart = echarts.init(document.getElementById(id), 'walden');

    myChart.setOption(option);

    return myChart;
}


/******************************************************************************
 *
 * 饼图
 *
 * ***************************************************************************/
EchartsUtil.pie = function (id, data, option) {
    var names = [];
    for (var i = 0; i < data.length; i++) {
        names.push(data[i].name);
    }

    var _option_ = {
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b}: {c} ({d}%)"
        },
        legend: {
            left: 'left',
            top: 'bottom',
            orient: 'vertical',
            data: names
        },
        series: [
            {
                name: '',
                type: 'pie',
                radius: ['20%', '40%'],
                avoidLabelOverlap: false,
                label: {
                    emphasis: {
                        show: true,
                        textStyle: {
                            fontSize: '30',
                            fontWeight: 'bold'
                        }
                    },
                    formatter: '{b}: {d}%'
                },
                labelLine: {
                    normal: {
                        show: true
                    }
                },
                data: data
            }
        ]
    };

    option = $.extend(true, {}, _option_, option);

    var myChart = echarts.init(document.getElementById(id), 'walden');

    myChart.setOption(option);

    return myChart;
}


/******************************************************************************
 *
 * 省内迁徙图
 *
 * ***************************************************************************/
EchartsUtil.migrateProvince = function (id, data, areaName, dType, option) {
    // 数据处理
    var mdata = EchartsUtil.migrateDataDeal(data, areaName, dType);

    // 数据注入
    option = $.extend(true, {}, option_migrate_base, option);
    option.series[1]["data"] = EchartsUtil.migrateDataConvert(mdata, geoCoordMap);
    option.series[2]["data"] = mdata.map(function (dataItem) {
        var from = 0;
        var to = 1;
        if ("out" == dType) {
            from = 1;
            to = 0;
        }

        return {
            name: dataItem[from].name,
            value: geoCoordMap[dataItem[from].name].concat(dataItem[from].value)
        }
    });
    option.series[0]["data"] = mdata.map(function (dataItem) {
        return {
            name: areaName,
            value: geoCoordMap[areaName].concat(0)
        }
    });
    option.geo.map = "shanxi";
    option.dataRange.max = migreateMaxNum;

    var myChart = echarts.init(document.getElementById(id), 'walden');
    myChart.setOption(option);

    return myChart;
}


/******************************************************************************
 *
 * 国内迁徙图
 *
 * ***************************************************************************/
EchartsUtil.migrateChina = function (id, data, areaName, dType, option) {
    // 数据处理
    var mdata = EchartsUtil.migrateDataDeal(data, areaName, dType);

    // 数据注入
    option = $.extend(true, {}, option_migrate_base, option);
    option.series[1]["data"] = EchartsUtil.migrateDataConvert(mdata, geoCoordMap);
    option.series[2]["data"] = mdata.map(function (dataItem) {
        var from = 0;
        var to = 1;
        if ("out" == dType) {
            from = 1;
            to = 0;
        }

        return {
            name: dataItem[from].name,
            value: geoCoordMap[dataItem[from].name].concat(dataItem[from].value)
        }
    });
    option.series[0]["data"] = mdata.map(function (dataItem) {
        return {
            name: areaName,
            value: geoCoordMap[areaName].concat(0)
        }
    });
    option.geo.map = "china";
    option.dataRange.max = migreateMaxNum;

    var myChart = echarts.init(document.getElementById(id), 'walden');
    myChart.setOption(option);

    return myChart;
}


/******************************************************************************
 *
 * 国际迁徙图
 *
 * ***************************************************************************/
EchartsUtil.migrateWorld = function (id, data, areaName, dType, option) {
    // 数据处理
    var mdata = EchartsUtil.migrateDataDeal(data, areaName, dType);

    // 数据注入
    option = $.extend(true, {}, option_migrate_base, option);
    option.series[1]["data"] = EchartsUtil.migrateDataConvert(mdata);
    option.series[2]["data"] = mdata.map(function (dataItem) {
            var from = 0;
            var to = 1;
            if ("out" == dType) {
                from = 1;
                to = 0;
            }

            return {
                name: dataItem[from].name,
                value: geoCoordMap[dataItem[from].name].concat(dataItem[from].value)
            }
        }
    );
    option.series[0]["data"] = mdata.map(function (dataItem) {
        return {
            name: areaName,
            value: geoCoordMap[areaName].concat(0)
        }
    });
    option.geo.map = "world";
    option.dataRange.max = migreateMaxNum;
    option.geo.label.normal.show = false; // 不显示国家名称，避免页面杂乱

    var myChart = echarts.init(document.getElementById(id), 'walden');
    myChart.setOption(option);

    return myChart;
}


/******************************************************************************
 *
 * 迁徙图，通用数据处理
 *
 * ***************************************************************************/
EchartsUtil.migrateDataDeal = function (data, areaName, dType) {
    var mdata = [];
    var maxarr = [];
    data.forEach(function (item, i) {
        var value = item.value;
        value = parseInt(value);

        var tmp = [];
        if ("out" == dType) {// 迁出
            tmp.push({
                "name": areaName,
                "value": value
            });
            tmp.push(item);
        } else {// 迁入
            tmp.push(item);
            tmp.push({
                "name": areaName,
                "value": value
            });
        }

        mdata.push(tmp);
        maxarr.push(value);
    });
    migreateMaxNum = Math.max.apply(null, maxarr);

    return mdata;
}


/******************************************************************************
 *
 * 迁徙图-通用数据转换
 *
 * ***************************************************************************/
EchartsUtil.migrateDataConvert = function (data) {
    var res = [];
    for (var i = 0; i < data.length; i++) {
        var dataItem = data[i];
        var fromCoord = geoCoordMap[dataItem[0].name];
        var toCoord = geoCoordMap[dataItem[1].name];
        if (fromCoord && toCoord) {
            res.push({
                fromName: dataItem[0].name,
                toName: dataItem[1].name,
                coords: [fromCoord, toCoord],
                value: dataItem[1].value
            });
        }
    }
    return res;
};


/******************************************************************************
 *
 * 迁徙图配置参数
 *
 * ***************************************************************************/
var option_migrate_base_color = ['#ff3333', 'orange', 'lime', 'aqua'];
var option_migrate_base = {
    backgroundColor: '#00000000',
    title: {
        left: 'center',
        textStyle: {
            color: '#fff'
        }
    },
    tooltip: {
        trigger: 'item'
    },
    legend: {
        orient: 'vertical',
        top: 'bottom',
        left: 'right',
        textStyle: {
            color: '#fff'
        },
        selectedMode: 'single'
    },
    dataRange: {
        min: 0,
        max: 100,
        calculable: true,
        color: ['#ff3333', 'orange', 'yellow', 'lime', 'aqua'],
        textStyle: {
            color: '#fff'
        }
    },
    geo: {
        map: 'china',
        label: {
            normal: {
                show: true,
                textStyle: {
                    color: 'rgba(0,0,0,0.6)'
                }
            }
        },
        roam: false,
        itemStyle: {
            normal: {
                areaColor: 'rgba(40,75,146,0.8)',
                borderColor: 'rgba(0,0,0,0.6)',
            },
            emphasis: {
                areaColor: '#2a333d'
            }
        }
    },
    series: [{
        type: 'effectScatter',
        coordinateSystem: 'geo',
        zlevel: 2,
        rippleEffect: {
            brushType: 'stroke'
        },
        label: {
            normal: {
                show: true,
                position: 'right',
                formatter: '{b}'
            }
        },
        symbolSize: 4,
        itemStyle: {
            normal: {
                color: 'aqua'
            }
        }
    }, {
        type: 'lines',
        zlevel: 2,
        symbol: ['none', 'none'],
        symbolSize: 10,
        effect: {
            show: true,
            period: 6,
            trailLength: 0,
            symbol: 'pin',
            symbolSize: 14
        },
        lineStyle: {
            normal: {
                color: function (item) {
                    var value = item.value;
                    if (value > parseInt(migreateMaxNum * 0.8)) {
                        return option_migrate_base_color[0];
                    } else if (value > parseInt(migreateMaxNum * 0.6)) {
                        return option_migrate_base_color[1];
                    } else if (value > parseInt(migreateMaxNum * 0.4)) {
                        return option_migrate_base_color[2];
                    } else {
                        return option_migrate_base_color[3];
                    }
                },
                width: 1,
                opacity: 0.6,
                curveness: 0.2
            }
        },
    }, {
        type: 'effectScatter',
        coordinateSystem: 'geo',
        zlevel: 2,
        rippleEffect: {
            brushType: 'stroke'
        },
        label: {
            normal: {
                show: true,
                position: 'right',
                formatter: '{b}'
            }
        },
        symbolSize: function (item) {
            var value = item[2];
            return parseInt(value / migreateMaxNum * 20);
        },
        itemStyle: {
            normal: {
                color: function (item) {
                    var value = item.value;
                    if (value > parseInt(migreateMaxNum * 0.8)) {
                        return option_migrate_base_color[0];
                    } else if (value > parseInt(migreateMaxNum * 0.6)) {
                        return option_migrate_base_color[1];
                    } else if (value > parseInt(migreateMaxNum * 0.4)) {
                        return option_migrate_base_color[2];
                    } else {
                        return option_migrate_base_color[3];
                    }
                }
            }
        }
    }]
};
